package mall.common.enums;

import mall.common.constants.SysConstants;

import java.util.StringJoiner;

/**
 * 促销活动类型 枚举
 * @author zmy
 * @since 2025/6/29 22:15
 */
public enum PromotionTypeEnum {
    ALL("00", "全场"),
    CATEGORY("01", "一类商品"),
    PRODUCT("02", "单个商品");

    /** 类型代码 */
    public final String type;
    /** 类型描述 */
    private final String label;

    PromotionTypeEnum(String type, String label) {
        this.type = type;
        this.label = label;
    }

    /**
     * 根据 type 获取枚举
     * @param type  类型代码
     * @return  PromotionTypeEnum：对应的枚举
     */
    public static PromotionTypeEnum getByType(String type) {
        for (PromotionTypeEnum value : values()) {
            if (value.type.equals(type)) return value;
        }
        throw new RuntimeException(SysConstants.MSG_SHOP_TYPE_NON);
    }

    /**
     * 根据类型 type 获取对应的 label
     * @param type  type 代码
     * @return  String：type类型对应的label
     */
    public static String getLabelByType(String type) {
        return getByType(type).label;
    }

    /** 查看所有类型 */
    public static String showAll() {
        StringJoiner sj = new StringJoiner("；");
        for (PromotionTypeEnum value : values()) {
            if (value.type.equals(PromotionTypeEnum.CATEGORY.type)) continue;
            String msg = "code：" + value + "，类型：" + value.label;
            System.out.println(msg);
            sj.add(msg);
        }
        return sj.toString();
    }

    @Override
    public String toString() {
        return "PromotionTypeEnum{" +
                "type='" + type + '\'' +
                ", label='" + label + '\'' +
                "} " + super.toString();
    }
}
